package com.ming.luckyDemo.controller;

import com.ming.luckyDemo.pojo.DO.UserInfoDO;
import com.ming.luckyDemo.pojo.TDO.BetDTO;
import com.ming.luckyDemo.pojo.entity.ResultBody;
import com.ming.luckyDemo.service.QueryLuckyService;
import com.ming.luckyDemo.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@SuppressWarnings("all")
@RestController
public class QueryLuckyController {
    //比较开奖结果
    //更新余额

    @SuppressWarnings("all")
    @Autowired
    private QueryLuckyService queryLuckyService;

    @PostMapping("/queryLucky")
    public ResultBody queryLucky(HttpServletRequest request, int numbers){
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        NumberUtil numberUtil = new NumberUtil();
        ArrayList<String> s = new ArrayList<>();

        Cookie[] cookies = request.getCookies();
        if (Objects.isNull(cookies)){
            return ResultBody.error().message("未登录");
        }
        for (Cookie cookie : cookies){
            UserInfoDO userInfoDO = queryLuckyService.login2(cookie.getName());
            if (cookie.getName().equals(userInfoDO.getUsername()) && cookie.getValue().equals(userInfoDO.getSessionKey())) {
                BetDTO dto = queryLuckyService.setBetDTO(numbers);
//                本局没有下注或刚好正负抵消，则随机
                if (dto.getZSize()==0&dto.getZParity()==0&dto.getSize1()==0&dto.getParity1()==0&dto.getSize2()==0&dto.getParity2()==0&dto.getSize3()==0&dto.getParity3()==0){
                    String random = queryLuckyService.getRandom();
                    queryLuckyService.saveLucky(numbers,random);
                    if (cookie.getName().equals("123")){
                        List<String> name = queryLuckyService.getName();
                        for (String s1 : name) {
                            System.out.println(s1);
                            //更新balance
                            queryLuckyService.updateBalance(s1,numbers);
                        }
                    }
                    return ResultBody.ok().message(numbers+"期没有下注或刚好正负抵消，则生成随机数").data("Lucky",random);
                }
                double[] doubles = queryLuckyService.seekLucky(dto);
                if (cookie.getName().equals("123")){
                    int a = (int)doubles[0];
                    queryLuckyService.saveLucky(numbers, numberUtil.getDigit(a));
                    List<String> name = queryLuckyService.getName();
                    for (String s1 : name) {
                        System.out.println(s1);
                        //更新balance
                        queryLuckyService.updateBalance(s1,numbers);
                    }
                }
                s.add(numberUtil.getDigit((int)doubles[0]));
                s.add(decimalFormat.format(0-doubles[3]));
                s.add(numberUtil.getDigit((int)doubles[1]));
                s.add(decimalFormat.format(0-doubles[4]));
                s.add(numberUtil.getDigit((int)doubles[2]));
                s.add(decimalFormat.format(0 -doubles[5]));

                return ResultBody.ok().code(200).message("根据下注单统计，"+numbers+"期最佳开奖为："+numberUtil.getDigit((int)doubles[0])+"，网站可获利"+decimalFormat.format(0-doubles[3])+"元 "+
                        "本期第二佳开奖为："+numberUtil.getDigit((int)doubles[2])+"，网站可获利"+decimalFormat.format(0-doubles[5])+"元 "+
                        "本期第三佳开奖为："+numberUtil.getDigit((int)doubles[1])+"，网站可获利"+decimalFormat.format(0-doubles[4])+"元 ").data("list",s);
            }
        }
        return null;
    }
}
